/*
 * Copyright @ 2015 Goldpac Co. Ltd. All right reserved.
 * @fileName SocketHelper.java
 * @author rui.kang
 */
package com.goldpac.jssb.common;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * Socket客户端管理类
 * @ClassName: SocketHelper
 * @author rui.kang
 * @version 1.0<br />
 * @Date 2017年5月16日 下午8:37:21<br />
 * @Logs <br />
 * ****************************************************<br />
 */
public class SocketHelper {
    private String  serverIp;
    private Integer port;
    private Socket  socket;

    public SocketHelper(String serverIp, Integer port) {
        super();
        this.serverIp = serverIp;
        this.port = port;
    }

    /**
     * 创建Socket
     * 
     * @Title: createSocket
     * @return
     */
    public boolean createSocket() {
        try {
            socket = new Socket(this.serverIp, this.port);
            socket.setTcpNoDelay(true);
            socket.setSoTimeout(60 * 1000 * 2);
            socket.setSendBufferSize(8096);
            socket.setReceiveBufferSize(8096);
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 发送socket消息
     * @Title: sendMessage
     * @param message
     * @return
     */
    public String sendMessage(String message) {
        OutputStream outputStream = null;
        InputStream inputStream = null;
        String response = null;
        try {
            /**
             * 发送TCP请求
             */
            outputStream = socket.getOutputStream();
            outputStream.write(message.getBytes("utf-8"));
//            outputStream.flush();
            /**
             * 接收TCP响应
             */
            inputStream = socket.getInputStream();
            ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();  
            byte[] buffer = new byte[512];  
            int len = -1;  
            while((len = inputStream.read(buffer)) != -1){  
                bytesOut.write(buffer, 0, len);
                if (buffer[len - 1] == (byte) 0x0A) {       // END flag
                    break;
                }
            }  
            /**
             * 解码TCP响应的完整报文
             */
            response = bytesOut.toString("UTF-8");
            
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return response;
    }

    /**
     * close socket
     * 
     * @Title: closeSocket
     * @return
     */
    public boolean closeSocket() {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            socket = null;
        }
        return true;
    }

    public String getServerIp() {
        return serverIp;
    }

    public void setServerIp(String serverIp) {
        this.serverIp = serverIp;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }
}
